package com.bjbn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bjbn.bean.IacUser;
import com.bjbn.bean.Organization;
import com.bjbn.entity.Organize;
import com.bjbn.service.IacUserService;
import com.bjbn.service.KafkaProducerService;
import com.bjbn.service.OrganizationService;
import com.bjbn.service.OrganizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class IndexController {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private OrganizeService organizeService;

    @Autowired
    private IacUserService iacUserService;
    @Autowired
    private KafkaProducerService kafkaProducerService;

    @RequestMapping("/hello")
    public String index() {
        return "Hello World!";
    }

    @RequestMapping("/organization/list")
    public List<Organization> test(@RequestParam(value = "modifiedDate", required = false) String modifiedDate) {
        return organizationService.getOrganizationList(modifiedDate);
    }

    @RequestMapping("/organization/update")
    public void updateLastOrganization(@RequestParam(value = "modifiedDate", required = false) String modifiedDate) {
        organizationService.updateOrganizationAll(modifiedDate);
    }

    @RequestMapping("/user/list")
    public List<IacUser> getAllIacUser(@RequestParam(value = "modifiedDate", required = false) String modifiedDate,
                                       @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                       @RequestParam(value = "pageSize", required = false, defaultValue = "2000") Integer pageSize
    ) {
        return iacUserService.getAllIacUser(modifiedDate, pageNumber, pageSize);
    }

    @RequestMapping("/user/update")
    public void updateLastUser(@RequestParam(value = "modifiedDate", required = false) String modifiedDate) {
        iacUserService.updateIacUserAll(modifiedDate);
    }

    @RequestMapping("/organize/")
    public List<Organize> test2() {
        return organizeService.getAllOrganize();
    }

    @GetMapping("/organize/{id}")
    public ResponseEntity<Organize> getUserById(@PathVariable String id) {
        Organize organize = organizeService.getItemById(id);
        return ResponseEntity.ok(organize);
    }

    @RequestMapping("/organization/send-list")
    public List<Organization> sendList() {
        String test = "[\n" +
                "  {\n" +
                "    \"id\": \"2001A110000000000DLP\",\n" +
                "    \"orgCode\": \"221402\",\n" +
                "    \"orgName\": \"航食科技公司\",\n" +
                "    \"fullName\": null,\n" +
                "    \"parentId\": \"0001A110000000000GOV\",\n" +
                "    \"parentCode\": null,\n" +
                "    \"parentName\": \"广东省机场管理集团有限公司\",\n" +
                "    \"type\": \"company\",\n" +
                "    \"status\": 1,\n" +
                "    \"wxOrgId\": 1544\n" +
                "  },\n" +
                "  {\n" +
                "    \"id\": \"2001A110000000000DLR\",\n" +
                "    \"orgCode\": \"221403\",\n" +
                "    \"orgName\": \"安全设备部\",\n" +
                "    \"fullName\": null,\n" +
                "    \"parentId\": \"2001A110000000000DLP\",\n" +
                "    \"parentCode\": null,\n" +
                "    \"parentName\": \"航食科技公司\",\n" +
                "    \"type\": \"department\",\n" +
                "    \"status\": 1,\n" +
                "    \"wxOrgId\": 1545\n" +
                "  }\n" +
                "]";

        List<Organization> organizations = JSONObject.parseArray(test, Organization.class);
        kafkaProducerService.sendBatchMessages("IAC_ORGANIZATION_UPDATE_DATA_OUT", organizations);

        return organizations;
    }

    @RequestMapping("/user/send-list")
    public List<IacUser> sendUserList() {
        String test = "[\n" +
                "  {\n" +
                "    \"gender\": \"2\",\n" +
                "    \"displayName\": \"方振-胡鹏\",\n" +
                "    \"departmentId\": \"1001A1100000001EQONS\",\n" +
                "    \"jobTitle\": \"经理\",\n" +
                "    \"mobile\": \"13430322415\",\n" +
                "    \"jobLevel\": \"三级正\",\n" +
                "    \"userState\": null,\n" +
                "    \"organization\": \"航食科技公司\",\n" +
                "    \"id\": \"0001A110000000000YYY\",\n" +
                "    \"pictureBase64\": null,\n" +
                "    \"userType\": \"合同工\",\n" +
                "    \"department\": \"运行采购部\",\n" +
                "    \"email\": \"hupeng@gdairport.com\",\n" +
                "    \"startWorkDate\": null,\n" +
                "    \"username\": \"18623053307\",\n" +
                "    \"status\": \"1\"\n" +
                "  }\n" +
                "]";

        List<IacUser> iacUsers = JSONObject.parseArray(test, IacUser.class);
        kafkaProducerService.sendBatchMessages("IAC_USER_UPDATE_DATA_OUT", iacUsers);
        return iacUsers;
    }

}
